package org.ldx.lotterysystem.service.activitystatus.operator;

import jakarta.annotation.Resource;
import org.ldx.lotterysystem.dao.dataobject.ActivityPrizeDo;
import org.ldx.lotterysystem.dao.mapper.ActivityPrizeMapper;
import org.ldx.lotterysystem.service.dto.ConvertActivityStatusDTO;
import org.ldx.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.springframework.stereotype.Component;

/**
 * @author 帘低晓露湿
 * @date 2025/11/13 11:04
 */
@Component
public class PrizeOperator extends AbstractActivityOperator{
    @Override
    public Integer sequence() {
        return 1;
    }
    @Resource
    ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum prizeStatusEnum = convertActivityStatusDTO.getTargetPrizeStatus();
        if(null == prizeId||
        null == prizeStatusEnum){
            return false;
        }
        ActivityPrizeDo activityPrizeDo = activityPrizeMapper.selectByAPId(convertActivityStatusDTO.getActivityId(), prizeId);
        if(null == activityPrizeDo){
            return false;
        }
        //奖品状态和目标状态相同，不需要更新
        if(activityPrizeDo.getStatus().equalsIgnoreCase(prizeStatusEnum.name())){
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum prizeStatusEnum = convertActivityStatusDTO.getTargetPrizeStatus();
        try{
            activityPrizeMapper.updateStatus(activityId,prizeId,prizeStatusEnum.name());
            return true;
        }catch (Exception e){
            return false;
        }


    }
}
